
#ifndef KBE_PACKET_READER_H
#define KBE_PACKET_READER_H

#include "memorystream.h"
// #include "helper/debug_helper.h"
// #include "network/common.h"

class Channel;
class Packet;
/*class MessageHandlers;*/

class PacketReader
{
public:
	enum PACKET_READER_TYPE
	{
		PACKET_READER_TYPE_SOCKET = 0,
		PACKET_READER_TYPE_WEBSOCKET = 1
	};

	PacketReader(Channel* pChannel);
	virtual ~PacketReader();

	virtual void reset();
	
	virtual bool processMessages(/*KBEngine::MessageHandlers* pMsgHandlers,*/ Packet* pPacket);


	virtual PacketReader::PACKET_READER_TYPE type()const { return PACKET_READER_TYPE_SOCKET; }

	bool hasverifydata() const { return (readFlag & READ_FLAG_VERIFY )> 0; }
	bool checkverifydata();

protected:
	enum FragmentDataTypes
	{
		FRAGMENT_DATA_UNKNOW,
		FRAGMENT_DATA_MESSAGE_LENGTH,
		FRAGMENT_DATA_MESSAGE_TYPE,
		FRAGMENT_DATA_MESSAGE_FLAG,
		FRAGMENT_DATA_MESSAGE_TAGID,
		FRAGMENT_DATA_MESSAGE_VERIFY,
		FRAGMENT_DATA_MESSAGE_BODY
	};

	enum FragmentDataREAD_FLAG
	{
		READ_FLAG_NONE =0,
		READ_FLAG_MSGLENGTH = 1,
		READ_FLAG_VERIFY = 2,
		READ_FLAG_BODY = 4
	};
	
	virtual void writeFragmentMessage(FragmentDataTypes fragmentDatasFlag, Packet* pPacket, uint32 datasize);
	virtual void mergeFragmentMessage(Packet* pPacket);

protected:
	uint8*						pFragmentDatas_;
	uint32						pFragmentDatasWpos_;
	uint32						pFragmentDatasRemain_;
	FragmentDataTypes			fragmentDatasFlag_;
	MemoryStream*				pFragmentStream_;
	uint32						readFlag;

	//MessageID			currMsgID_;
	uint8 a;
	uint8 b;
	uint8 c;
	uint8 d;
	UINT32						msgLength;
	UINT32						msgType;
	UINT32						msgFlag;
	UINT32						msgTagID;  //only rpc
	
	Channel*					pChannel_;
};

#endif 
